Okay so lets quickly recap where we are at with this project:

  • We have built the 'board function'
  • We can print the board.
  • We can set the board to display the correct number of neughor bombs.

And finally, let's quickly consider what we have left to do:

  • create a 'reveal square' function.
  • create a 'flag square' function.
  • Add win/loss checks, a UI, set-up functions, bling
  • Bring everything together

Today we shall be focus on the reveal square function.

ToDo

Okay, so here's the basic implementation idea. When we start a new game we create two boards:

  • The 'game board' (with bombs) -- Hidden from player
  • The 'player board' (with flags and numbers) -- This is what the players sees during the game.

The player board will have the same dimensions as the game board. This means that the square (0, 0) on the game board corressponds to same square on the player board. You may remember that when we wrote the 'build_board' function I advised you to make it generalisable. Well, the effort is about to pay-off; we can create both of our boards with just a few lines of code:


In [ ]:
NUMBER_OF_ROWS  = 3
NUMBER_OF_COLS  = 3
NUMBER_OF_BOMBS = 2 # You may remember that all_caps mean that these variables should NOT change values at runtime.

game_board = build_board(NUMBER_OF_ROWS, NUMBER_OF_COLS, bomb_count = NUMBER_OF_BOMBS)
player_board = build_board(NUMBER_OF_ROWS, NUMBER_OF_COLS, bomb_count=0)

The Reveal function

  • Take a square as input and reveal a square to the player (said square will be either a bomb or a number)
  • In the case of revealing a bomb, its game over.
  • If we reveal a number, it should stay revealed until the end of the game.
  • If we highlight an already selected square (e.g a number or the letter "F") we should do nothing.

The Flag Function

  • If the selected square is flagged, de-flag it.
  • If the square is already revealed (e.g. a flag or a number) we should do nothing.
  • If we flag a square, the player board should display an "F" character.

Also note that if the player flags all (and only) bombs then the player has won the game. You may wish to bear that in mind as you write your functions...

Also as you read the requirements it should hopefully occur to you that the flag and reveal square functions are actually rather similar. What do this mean? What should you do as a result of this similiarity?


In [ ]:
def test_board():
    """
    Write your tests here...
    
    Example:
    >>> 1 + 1
    2
    
    """
    import doctest
    doctest.testmod()
    print("TESTING COMPLETE... if you see nothing, (other than this message) that means all tests passed.")

In [ ]:
def reveal_square(row, col, player_board, game_board):
    # Your code here...
    pass

# testing...
test()

In [ ]:
def flag_square(row, col, player_board):
    # Your code here...
    pass

# testing...
test()